﻿using System.Drawing;
using GraphDrawing2.V2.Concrete.Model;

namespace GraphDrawing2.V2.Graph.Model
{
    public class ConnectableElement : VisualElement<ConnectableElementModel, IConnectableElementView>, IZOrdered
    {
        protected override ConnectableElementModel CreateModel()
        {
            return new ConnectableElementModel();
        }

        protected override IConnectableElementView CreateView()
        {
            return new ConnectableElementView();
        }

        protected override IHitTestResult HitTestInternal(PointF localPoint)
        {
            var connectorLocal = localPoint.RelativeToOrigin(model.InputConnector.Position);
            if (view.IsInputHit(connectorLocal))
                return new ConnectorHitTest(new ConnectorOrigin(model, model.InputConnector));
            connectorLocal = localPoint.RelativeToOrigin(model.OutputConnector.Position);
            if (view.IsOutputHit(connectorLocal))
                return new ConnectorHitTest(new ConnectorOrigin(model, model.OutputConnector));
            return view.Contains(localPoint) ? new ElementHitTest(this) : (IHitTestResult) NoneHitTest.INSTANCE;
        }

        public void BringToFront()
        {
            Owner.BringToFront(this);
        }
    }

    public class ConnectableElementModel : VisualElementModel
    {
        public int Width { get; set; }
        public int Height { get; set; }
        public ConnectorModel InputConnector { get; private set; }
        public ConnectorModel OutputConnector { get; private set; }

        public ConnectableElementModel()
        {
            Width = 60;
            Height = 80;
            const int radius = 5;
            InputConnector = new ConnectorModel(radius);
            InputConnector.Position = new PointF(-radius, Height/2f - radius);
            OutputConnector = new ConnectorModel(radius);
            OutputConnector.Position = new PointF(Width - radius, Height/2f - radius);
        }
    }

    public class ConnectorModel : IVisualItem
    {
        public PointF Position { get; set; }
        public StateHints Hints { get; set; }
        public RectangleF Bounds { get; private set; }

        public ConnectorModel(int radius)
        {
            Bounds = new RectangleF(0, 0, radius*2, radius*2);
        }
    }
}